package com.hmall.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    private final static String CREATE_TIME = "createdAt";
    private final static String UPDATE_TIME = "updatedAt";

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, CREATE_TIME, LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, UPDATE_TIME, LocalDateTime.class, LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("开始更新时间填充");
//        this.strictUpdateFill(metaObject, "updatedAt", LocalDateTime.class, LocalDateTime.now()); //这种方式不生效
        log.info("更新时间填充结束");
//        // 更新的时候更新时间直接修改
        setFieldValByName(UPDATE_TIME, LocalDateTime.now(), metaObject);
//
//        // 跟新不允许修改创建时间字段
//        setFieldValByName("createdAt", null, metaObject);
    }
}